package com.myebills.ebpsfrontend.cust.maxis;

import java.sql.*;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.myebills.ebpsfrontend.cust.controller.*;
import javax.servlet.http.HttpSession;
import com.ibm.logging.ILogger;
import com.ibm.logging.IRecordType;
import java.net.URLDecoder;

public class CusPaymentReceiptServlet extends EbppxTransactionServlet {

    public void doTransaction(HttpServletRequest req, HttpServletResponse res) throws ServletException {

        ILogger trcLogger = null;
        trcLogger = getTraceLogger(req);
        final String fName = "doTransaction";

        if (trcLogger.isLogging())
            trcLogger.entry(IRecordType.TYPE_INFO, this, fName);

        final HttpSession session = req.getSession(false);
        if (null == session)
                throw new IllegalStateException("no session established");

        //Get attribute from session for the logon user
        final EbppxEcsUser ecsUser = (EbppxEcsUser) session.getAttribute(EbppxConstants.ECS_USER);
	    String strEmail = ecsUser.getEmail();


//http://localhost:9090/cust/PaymentReceipt?accountReference=Credit+Card+External%7C1080049%7Cashwini&actionPrefix=null&locale=en_MY_INDIVIDUAL&deviceFingerprint=&txnResponseCode=1&customerEmail=dada%40dx.com&systemPassword=qKW4IiwEUv57XRU&orderInfo=BillPayment&amount=2590&timestamp=20121009221450307&action=externalpostPaidBill&productQuantity=&profileCode=INDIVIDUAL&ipAddress=127.0.0.1&billpaymentMSISDN_or_AccNo=814242186&productIdentifierCode=&orderID=288700891&inputfieldType=ACCT&hash=dbcdf14c825736b7e73050dab1b6e1dd916c86fa2e4a7a3e2fac218dcc5337a5&Message=Payer+amount+too+low+to+transact.&receiptNo=&paymentType=Kenan&merchantID=2&merchTxnRef=288700891&txnNo=&nrcID=&systemID=MOA0001&productName=&paymentReturnURL=http%3A%2F%2Flocalhost%3A9090%2Fcust%2FPaymentReceipt
//http://localhost:9090/cust/PaymentReceipt?ipAddress=127.0.0.1&billpaymentMSISDN_or_AccNo=814242186&productIdentifierCode=&orderID=288700891&inputfieldType=ACCT&hash=dbcdf14c825736b7e73050dab1b6e1dd916c86fa2e4a7a3e2fac218dcc5337a5&Message=Payer+amount+too+low+to+transact.&receiptNo=&paymentType=Kenan&merchantID=2&merchTxnRef=288700891&txnNo=&nrcID=&systemID=MOA0001&productName=&paymentReturnURL=http%3A%2F%2Flocalhost%3A9090%2Fcust%2FPaymentReceipt
        try {
			String merchantID = EbppxUtil.nullToEmpty(req.getParameter("merchantID"));
            if (merchantID.length() == 0) {
                throw new ServletException("Missing merchant Id");
            }

			String merchTxnRef = EbppxUtil.nullToEmpty(req.getParameter("merchTxnRef"));
            if (merchTxnRef.length() == 0) {
                throw new ServletException("Missing bill reference number");
            }

			String orderID = EbppxUtil.nullToEmpty(req.getParameter("orderID"));
			String orderInfo = EbppxUtil.nullToEmpty(req.getParameter("orderInfo"));
			String systemID = EbppxUtil.nullToEmpty(req.getParameter("systemID"));
			String amount = EbppxUtil.nullToEmpty(req.getParameter("amount"));
            if (amount.length() == 0) {
                throw new ServletException("Missing payment amount");
            }

			String billPaymentMSISDN = EbppxUtil.nullToEmpty(req.getParameter("billpaymentMSISDN_or_AccNo"));
			String paymentType = EbppxUtil.nullToEmpty(req.getParameter("paymentType"));
			String nrcID = EbppxUtil.nullToEmpty(req.getParameter("nrcID"));

			String hash = EbppxUtil.nullToEmpty(req.getParameter("hash"));
            if (hash.length() == 0) {
                throw new ServletException("Missing hash data");
            }
			String deviceFingerprint = EbppxUtil.nullToEmpty(req.getParameter("deviceFingerprint"));
			String productIdentifierCode = EbppxUtil.nullToEmpty(req.getParameter("productIdentifierCode"));
			String productName = EbppxUtil.nullToEmpty(req.getParameter("productName"));
			String productQuantity = EbppxUtil.nullToEmpty(req.getParameter("productQuantity"));
			String customerEmail = EbppxUtil.nullToEmpty(req.getParameter("customerEmail"));
			System.out.println("customerEmail->"+customerEmail);

			String IPaddress = EbppxUtil.nullToEmpty(req.getParameter("ipAddress"));
            if (IPaddress.length() == 0) {
                throw new ServletException("Missing IP address");
            }
			System.out.println("IPaddress->"+IPaddress);

			String timestamp = EbppxUtil.nullToEmpty(req.getParameter("timestamp"));
            if (timestamp.length() == 0) {
                throw new ServletException("Missing timestamp");
            }

			System.out.println("timestamp->"+timestamp);

			String message = EbppxUtil.nullToEmpty(req.getParameter("Message"));
            if (message.length() == 0) {
                throw new ServletException("Missing message");
            }
            System.out.println("Message:"+message);

			String receiptNo = EbppxUtil.nullToEmpty(req.getParameter("receiptNo"));
            /*if (receiptNo.length() == 0) {
                throw new ServletException("Missing receipt number");
            }*/
			String txnResponseCode = EbppxUtil.nullToEmpty(req.getParameter("txnResponseCode"));
			System.out.println("txnResponseCode->"+txnResponseCode);

			String txnNo = EbppxUtil.nullToEmpty(req.getParameter("txnNo"));
            /*if (txnNo.length() == 0) {
                throw new ServletException("Missing transaction number");
            }*/

			String hashSalt = EbppxUtil.getHashSalt(timestamp,IPaddress);
			System.out.println("Hashsalt->"+hashSalt);

			String hashData = hashSalt +  URLDecoder.decode(customerEmail.substring(0,5),"UTF-8") + URLDecoder.decode(message.substring(0,5),"UTF-8") + URLDecoder.decode(txnNo,"UTF-8");
			System.out.println("hashData"+hashData);

			String myHash = EbppxUtil.SHA256(hashData);

			System.out.println("myHash"+myHash);
			System.out.println("hash"+hash);

			if(!myHash.equals(hash)){
                throw new ServletException("Invalid hash code. EBPS hash is "+myHash+" and IPC hash is "+hash);
			}

			req.setAttribute("merchantID", merchantID);
			req.setAttribute("merchTxnRef", merchTxnRef);
			req.setAttribute("orderID", orderID);
			req.setAttribute("orderInfo", orderInfo);
			req.setAttribute("systemID", systemID);
			req.setAttribute("amount", amount);
			req.setAttribute("billPaymentMSISDN", billPaymentMSISDN);
			req.setAttribute("paymentType", paymentType);
			req.setAttribute("nrcID", nrcID);
			req.setAttribute("hash", hash);
			req.setAttribute("deviceFingerprint", deviceFingerprint);
			req.setAttribute("productIdentifierCode", productIdentifierCode);
			req.setAttribute("productName", productName);
			req.setAttribute("productQuantity", productQuantity);
			req.setAttribute("customerEmail", customerEmail);
			req.setAttribute("IPaddress", IPaddress);
			req.setAttribute("timestamp", timestamp);
			req.setAttribute("message", message);
			req.setAttribute("receiptNo", receiptNo);
			req.setAttribute("txnResponseCode", txnResponseCode);
			req.setAttribute("txnNo", txnNo);

			forwardToMappedUrl(req, res, EbppxConstants.URL_JSP_PARAM);
		}catch (Throwable t){
			throw new ServletException(t.toString(), t);
		}finally {

	    } //finally

    }//doTransaction



}
